package com.e3.web.widget.client;

import java.util.List;

import com.e3.web.core.model.SuperModel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.MultiField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.google.gwt.user.client.Element;

public abstract class MyGridField<D extends SuperModel> extends MultiField<D> implements MyField<D> {
	
	protected AdapterField gridAdapter;
	protected AdapterField buttonAdapter;
	protected VerticalPanel buttonBar;

	private Grid<D> gridPanel;
	private GridSelectionModel<D> gridSelectionModel;
	private ListStore<D> gridStore;

	private boolean isEditorGrid = false;
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	public MyGridField(boolean isEditorGrid){
		this.isEditorGrid = isEditorGrid;
		setHideLabel(true);
		
		gridAdapter = new AdapterField(getGridPanel());
		gridAdapter.setResizeWidget(true);
		add(gridAdapter);
		
		Button[] buttons = getButtons();
		if(buttons!=null&&buttons.length>0){
			buttonBar = new VerticalPanel();
			
			buttonBar.setSpacing(5);
			buttonBar.setStyleAttribute("margin", "3px");
			buttonBar.setHorizontalAlign(HorizontalAlignment.CENTER);
			buttonAdapter = new AdapterField(buttonBar);
			add(buttonAdapter);
		}
	}
	
	@Override
	protected void onRender(Element target, int index) {
		
		Button[] buttons = getButtons();
		if(buttons!=null&&buttons.length>0){
			initButtons(getButtons());
		}
		setSize(400, 300);
		super.onRender(target, index);
		
	}
	
	protected void initButtons(Button[] buttons) {
		int width = getButtonWidth();
		for (Button button : buttons) {
			button.setWidth(width);
			button.addSelectionListener(btnSelectListener);
			buttonBar.add(button);
		}
	}

	protected void onBtnMnuEvent(BaseAction action) {
		
	}

	public Grid<D> getGridPanel(){
		if(gridPanel == null){
			ColumnModel cm = new ColumnModel(getGridColumnConfigs());
			gridPanel = createGridPanel(getGridStore(),cm);
			gridPanel.setSelectionModel(getGridSelectionModel());
		}
		return gridPanel;
	}

	private Grid<D> createGridPanel(ListStore<D> store,ColumnModel cm){
		if(isEditorGrid){
			return new EditorGrid<D>(store,cm);
		}
		return new Grid<D>(store,cm);
	}
	
	public ListStore<D> getGridStore(){
		if(gridStore == null){
			gridStore =  new ListStore<D>();
		}
		return gridStore;
	}
	
	public GridSelectionModel<D> getGridSelectionModel(){
		if(gridSelectionModel==null){
			gridSelectionModel = new GridSelectionModel<D>();
		}
		return gridSelectionModel;
	}
	
	@SuppressWarnings("unchecked")
	public D getFieldValue() {
		return (D)getGridValues();
	}

	@SuppressWarnings("unchecked")
	public void setFieldValue(D value) {
		List<D> models = (List<D>)value;
		setGridValues(models);
	}
	
	public List<D> getSelectedModels(){
		return getGridSelectionModel().getSelectedItems();
	}
	
	public List<D> getGridValues(){
		return getGridStore().getModels();
	}
	
	public void setGridValues(List<D> models){
		getGridStore().removeAll();
		if(models!=null&&models.size()>0){
			getGridStore().add(models);
		}
	}
	
	public void addGridValue(D model){
		if(isEditorGrid){
			((EditorGrid<D>)getGridPanel()).stopEditing();  
			getGridStore().insert(model, 0);  
			((EditorGrid<D>)getGridPanel()).startEditing(0, 0);  
		}
		else{
			getGridStore().add(model);
		}
	}
	
	public void removeSelectedValues(){
		List<D> models = getSelectedModels();
		if(models!=null&&models.size()>0){
			for (D model : models) {
				getGridStore().remove(model);
			}
		}
		
	}
	
	protected int getButtonWidth(){
		return 100;
	}
	
	@Override
	protected void onResize(int width, int height) {
		super.onResize(width, height);
		if (orientation == Orientation.HORIZONTAL) {
			int w = (width - buttonAdapter.el().getParent().getWidth());
			w -= (fields.size() * spacing);
			getGridPanel().setSize(w, height);
		} else {
			for (Field<?> f : fields) {
				f.setWidth(width);
			}
		}
	}
	
	@Override
	public void clear() {
		super.clear();
		getGridStore().removeAll();
	}

	
	protected abstract List<ColumnConfig> getGridColumnConfigs() ;
	
	protected abstract Button[] getButtons();

}
